import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import vitePluginImp from 'vite-plugin-imp';
import path from 'path';
import fs from 'fs';
import lessToJS from 'less-vars-to-js';

const themeVariables = lessToJS(
  fs.readFileSync(
    path.resolve(__dirname, './src/assets/css/variables.less'),
    'utf8',
  ),
);

export default defineConfig({
  server: {
    host: true,
    port: 8080,
  },
  plugins: [
    react(),
    vitePluginImp({
      libList: [
        {
          libName: 'antd',
          style: (name) => `antd/lib/${name}/style/index.less`,
        },
      ],
    }),
  ],
  css: {
    preprocessorOptions: {
      less: {
        // 支持内联 JavaScript
        javascriptEnabled: true,
        // 重写 less 变量，定制样式
        modifyVars: themeVariables,
      },
    },
  },
  resolve: {
    alias: [
      {
        find: /@\//,
        replacement: path.join(__dirname, './src/'),
      },
    ],
  },
});
